﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AioNightly.Dao;
using AioNightly.Model;
using AioNightly.Common;

namespace AioNightly
{
    public partial class Admin : System.Web.UI.Page
    {
        private string m_defaultStr = "Please Select";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckUser();
                GetProducts();
            }
        }

        private User m_user;
        private void CheckUser()
        {
            m_user = (User)Session["User"];

            if (m_user == null)
            {
                Response.Redirect("~/Account/Login.aspx");
            }
        }

        private void GetProducts()
        {
            ProductDAO m_productDAO = new ProductDAO();
            Product[] m_productArray = m_productDAO.GetAllProducts().ToArray();
            ddl_product.DataSource = m_productArray;
            ddl_product.DataTextField = "Name";
            ddl_product.DataBind();
            StoreDefaultSelect();
        }

        private void StoreDefaultSelect()
        {
            if (ddl_product.Items.Count > 0)
            {
                if (ddl_product.Items[0].Value != m_defaultStr)
                {
                    ddl_product.Items.Insert(0, m_defaultStr);
                }
            }
            else
            {
                ddl_product.Items.Insert(0, m_defaultStr);
            }
        }

        protected void rp_newitem_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((HyperLink)e.Item.FindControl("hl_newitem")).Text = string.Format("{0}. {1}", e.Item.ItemIndex + 1, ((NewNote)e.Item.DataItem).Content);
                ((HyperLink)e.Item.FindControl("hl_newitem")).NavigateUrl = string.Format("~/EditNew.aspx?new={0}", ((NewNote)e.Item.DataItem).Id);
                ((HyperLink)e.Item.FindControl("hl_newitem")).ForeColor = System.Drawing.Color.Black;
            }
        }

        protected void ddl_product_SelectedIndexChanged(object sender, EventArgs e)
        {
            NewnoteDAO m_newnoteDAO = new NewnoteDAO();
            NewNote[] m_newnoteArray = m_newnoteDAO.GetNewnoteByProduct(ddl_product.SelectedValue).ToArray();
            rp_newitem.DataSource = m_newnoteArray;
            rp_newitem.DataBind();

            GetVersionByProductName(ddl_product.SelectedValue);
        }

        private void GetVersionByProductName(string productName)
        {
            PVersionDAO m_pversionDAO = new PVersionDAO();
            PVersion[] m_pversionArray = m_pversionDAO.GetVersionByProductName(productName).ToArray();
            ddl_version.DataSource = m_pversionArray;
            ddl_version.DataTextField = "VersionName";
            ddl_version.DataBind();

            if (ddl_version.Items.Count > 0)
            {
                if (ddl_version.Items[0].Value != m_defaultStr)
                {
                    ddl_version.Items.Insert(0, m_defaultStr);
                }
            }
        }

        protected void bt_move_Click(object sender, EventArgs e)
        {
            if (ddl_product.SelectedIndex!=0)
            {
                NewnoteDAO m_newnoteDAO = new NewnoteDAO();
                m_newnoteDAO.Move2Item(ddl_product.SelectedValue);
                m_newnoteDAO.ClearNewnoteByProductId(ddl_product.SelectedIndex);

                BuildDAO m_buildDAO = new BuildDAO();
                int buildid = m_buildDAO.GetIdByCreateTime(ddl_build.SelectedValue);

                if (buildid!=-1)
                {
                    ItemDAO m_itemDAO = new ItemDAO();
                    m_itemDAO.LabelItem2Build(buildid);
                }
            }
        }

        protected void ddl_version_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetBuildByVersion(ddl_version.SelectedValue);
        }

        private void GetBuildByVersion(string versionName)
        {
            BuildDAO m_buildDAO = new BuildDAO();
            Build[] m_buildArray = m_buildDAO.GetBuildByVersion(versionName).ToArray();
            ddl_build.DataSource = m_buildArray;
            ddl_build.DataTextField = "CreateTime";
            ddl_build.DataBind();

            if (ddl_build.Items.Count > 0)
            {
                if (ddl_build.Items[0].Value != m_defaultStr)
                {
                    ddl_build.Items.Insert(0, m_defaultStr);
                }
            }
        }

        protected void bt_addPVersion_Click(object sender, EventArgs e)
        {
            if (ddl_product.SelectedIndex>0 && tb_pversion.Text.Trim().Length>0)
            {
                PVersionDAO m_pversionDAO = new PVersionDAO();
                m_pversionDAO.AddPVersion(ddl_product.SelectedIndex, tb_pversion.Text.Trim());
            }
        }

        protected void bt_addbuild_Click(object sender, EventArgs e)
        {
            if (ddl_product.SelectedIndex>0 && ddl_version.SelectedIndex>0)
            {
                PVersionDAO m_pversionDAO = new PVersionDAO();
                int m_versionId = m_pversionDAO.GetVersioinIdByVersionName(ddl_version.SelectedValue);

                if (m_versionId!=-1)
                {
                    BuildDAO m_buildDAO = new BuildDAO();
                    m_buildDAO.AddBuildNow(m_versionId);
                }
            }
        }

        protected void bt_addproduct_Click(object sender, EventArgs e)
        {
            if (tb_pversion.Text.Trim().Length > 0)
            {
                ProductDAO m_productDAO = new ProductDAO();
                m_productDAO.AddProduct(tb_pversion.Text.Trim());
            }
        }
    }
}